package news.paper.tintuc.tinnong.docbao.doctin;

import news.paper.tintuc.tinnong.docbao.doctin.GCM.PopupAD;
import news.paper.tintuc.tinnong.docbao.doctin.GCM.SendGCM;
import news.paper.tintuc.tinnong.docbao.doctin.controller.Controller;
import news.paper.tintuc.tinnong.docbao.doctin.controller.RequestListener;
import news.paper.tintuc.tinnong.docbao.doctin.model.News;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;

import org.json.JSONObject;

import tienich360.com.lib.JsonConvert;
import tienich360.com.lib.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Window;

import com.google.analytics.tracking.android.EasyTracker;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.splash.SplashConfig;
import com.startapp.android.publish.splash.SplashConfig.Theme;

public class Activity_Splash extends Activity {
	private TelephonyManager telephonyManager;
	private ConnectivityManager connectivityManager;
	protected boolean _active = true;
	protected int _splashTime = 2000;

	// private static LayoutInflater inflater = null;

	// private String contentPopup;
	// private boolean isShow;
	// private String networkOperator = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.splash);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		// int height = displaymetrics.heightPixels;
		Constrant.WIDTH = displaymetrics.widthPixels;
		connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		Constrant.SUBID = Utils.getIMEI(telephonyManager);
		if (Utils.isConnected(telephonyManager, connectivityManager)) {

			PopupAD popupAD = new PopupAD();
			popupAD.getAppinfo(getPackageName(), new RequestListener() {

				@Override
				public void success(Object response) {
					PopupAD popupAD = new PopupAD();
					if (popupAD.showUpdate(Activity_Splash.this)) {
						popupAD.showPopupUpDate(Activity_Splash.this);
					} else
						load_data(Constrant.SUBID);
				}

				@Override
				public void fail(String message) {
					load_data(Constrant.SUBID);
				}

				@Override
				public void begin() {

				}
			});

		} else {
			showPopup("Kết nối mạng có vấn đề, bạn vui lòng kiểm tra lại kết nối");
		}
		SendGCM.registerGCM(Activity_Splash.this, Constrant.SUBID);
		int text_size = Utils.getSubidInt("TEXT_SIZE", this);
		if (text_size > 10)
			Constrant.TEXT_SIZE = text_size;
		else
			Constrant.TEXT_SIZE = 14;
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this method.
	}

	private void showPopup(String mess) {
		AlertDialog.Builder ab = new AlertDialog.Builder(Activity_Splash.this);
		ab.setMessage(mess);
		ab.setCancelable(false);
		ab.setPositiveButton("Kiểm tra", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				showPopupReConnect("Bạn có muốn tiếp tục sử dụng ứng dụng?");
				startActivity(new Intent(
						android.provider.Settings.ACTION_SETTINGS));
			}
		});
		ab.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				onDestroy();
				finish();
			}
		});
		ab.show();
	}

	private void showPopupReConnect(String mess) {
		AlertDialog.Builder ab = new AlertDialog.Builder(Activity_Splash.this);
		ab.setMessage(mess);
		ab.setCancelable(false);
		ab.setPositiveButton("Tiếp tục", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				if (Utils.isConnected(telephonyManager, connectivityManager)) {
					load_data(Constrant.SUBID);
				} else {
					showPopup("Kết nối mạng có vấn đề, bạn vui lòng kiểm tra lại kết nối");
				}
			}
		});
		ab.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				onDestroy();
				finish();
			}
		});
		ab.show();
	}

	private void load_data(String subid) {
		try {
			if (Constrant.arrayListNews != null) {
				Constrant.arrayListNews.clear();
			}
		} catch (Exception e) {
		}

		Controller controller = new Controller();
		controller.getListType(new RequestListener() {

			@Override
			public void success(Object response) {
				try {
					JSONObject jsonObject = (JSONObject) response;
					JSONObject jsonObject2 = jsonObject.getJSONObject("home");
					Constrant.arrayListNews = JsonConvert.DeserializeArray(
							News.class,
							jsonObject2.getJSONArray("ZoneSourceList"),
							Constrant.arrayListNews);
					Intent intent = new Intent(Activity_Splash.this,
							Activity_Menu.class);
					finish();
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void fail(String message) {
			}

			@Override
			public void begin() {

			}
		});
	}
}